﻿using System;
using UnityEngine;

public class Action1000 : GameAction
{
    public class Passport
    {
        public string PassportId { get; set; }
        public string Password { get; set; }
    }
    private ActionResult actionResult;

    public Action1000()
        : base(1000)
    {
    }

    protected override void SendParameter(NetWriter writer, ActionParam actionParam)
    {
        //登录服务器获取账号
        string handler = actionParam.Get<string>("Handler");
        writer.writeString("Handler", handler);
        writer.writeInt32("MobileType", GameSetting.Instance.MobileType);

        if (handler == "Regist" || handler == "Login")
        {
            if (handler == "Regist")
            {
                writer.writeString("IsCustom", "true");
            }
            else
            {
                writer.writeString("IMEI", GameSetting.Instance.DeviceID);
                writer.writeString("DeviceID", GameSetting.Instance.DeviceID);
            }
            writer.writeString("Pid", GameSetting.Instance.Pid);
            writer.writeString("Pwd", new DESAlgorithmNew().EncodePwd(GameSetting.Instance.Password, GameSetting.ClientPasswordKey));
        }
        else
        {
            writer.writeString("IMEI", GameSetting.Instance.DeviceID);
            writer.writeString("DeviceID", GameSetting.Instance.DeviceID);
        }
    }

    protected override void DecodePackage(NetReader reader)
    {
        var dt = reader.readValue<Login.Data>();
        actionResult = new ActionResult();
        actionResult["PassportId"] = dt.PassportId;
        actionResult["Password"] = (dt.Password == null || dt.Password == "") ? "" : new DESAlgorithmNew().DecodePwd(dt.Password, GameSetting.ClientPasswordKey);
        actionResult["UserID"] = dt.UserID;
        actionResult["Token"] = dt.Token;

        //TODO:登录服务器获取账号
        //var passport = reader.readValue<Passport>();
        //actionResult["passportID"] = passport.PassportId;
        //actionResult["password"] = passport.Password;

    }

    public override ActionResult GetResponseData()
    {
        return actionResult;
    }
}
